package com.whu.components;

import com.whu.UVAProtocol.UVAConnection;
import com.whu.UVAProtocol.UVAServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@Component
public class ClearDisconnectionAsyn {
   // * 0/5 * * * ? *
    //每半分钟检查一次重连队列，移除过期连接
    @Scheduled(cron = "0/30 * * * * ?")
    public void clearExpiredConnection(){
        if(UVAServer.disconnections.isEmpty()){
            return;
        }
        for (Map.Entry<Integer, UVAConnection> entry : UVAServer.disconnections.entrySet()) {
            UVAConnection connection = entry.getValue();
            if (LocalDateTime.now().isAfter(connection.getExpiredTime())) {
                //过期
                log.info("移除过期连接："+entry.getKey());
                UVAServer.disconnections.remove(entry.getKey());
            }
        }
    }
}
